import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'widget_sample_providers.dart';

// | Provider                | 最基本的provider类型，获取一个任何类型的数据值               |
// | ----------------------- | ------------------------------------------------------------ |
// | ListenableProvider      | 提供一个Listenable的指定对象，当侦听被调用时Listenable将侦听对象和重求创建请求组件 |
// | ChangeNotifierProvider  | 为ChangeNotifier提供的ListenableProvider，在需要时自动调用ChangeNotifier.dispose |
// | ValueListenableProvider | 侦听一个ValueListenable获取ValueListenable.value             |
// | StreamProvider          | 侦听一个Stream流并获取最新emitted插入值                      |

class SampleProviderDemoHome extends StatefulWidget {
  const SampleProviderDemoHome({Key? key}) : super(key: key);

  @override
  _SampleProviderDemoHomeState createState() => _SampleProviderDemoHomeState();
}

class _SampleProviderDemoHomeState extends State<SampleProviderDemoHome> {
  AddressListProvider _addressListProvider = AddressListProvider();

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: ChangeNotifierProvider<AddressListProvider>(
        create: (_) => _addressListProvider,
        child: Scaffold(
          appBar: AppBar(
            centerTitle: true,
            title: Text("Provider练习"),
          ),
          body: Container(
            padding: EdgeInsets.all(10),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                TextDisplay(),
                TextEditWidget(),
                RaisedButton(
                  onPressed: () {
                    _addressListProvider.fetchUserList();
                  },
                  child: Text('Fetch Data from Network'),
                ),
                AddressListWidget()
              ],
            ),
          ),
        ),
      ),
    );
  }
}
